package com.zuo.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zuo.seckilldemo.entity.GoodsSeckill;
import com.zuo.seckilldemo.entity.Order;
import com.zuo.seckilldemo.entity.OrderInfo;
import com.zuo.seckilldemo.entity.User;
import com.zuo.seckilldemo.mapper.OrderInfoMapper;
import com.zuo.seckilldemo.mapper.OrderMapper;
import com.zuo.seckilldemo.service.GoodsSeckillService;
import com.zuo.seckilldemo.service.OrderInfoService;
import com.zuo.seckilldemo.utils.MD5Util;
import com.zuo.seckilldemo.utils.UUIDUtil;
import com.zuo.seckilldemo.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuogaocan
 * @since 2023-01-31
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private GoodsSeckillService goodsSeckillService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public Long getResult(User user, Long goodsId) {
        OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("user_id", user.getId()).eq("goods_id", goodsId));
        if (orderInfo !=null){
            return orderInfo.getId();
        }
        else if (redisTemplate.hasKey("stockEmpty:"+goodsId)) return -1L;
        else return 0L;
    }

    @Transactional
    @Override
    public OrderInfo seckill(User user, GoodsVo goodsVo) {
        GoodsSeckill seckillGoods = goodsSeckillService.getOne(new QueryWrapper<GoodsSeckill>().eq("goods_id", goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
        boolean flag = goodsSeckillService.update(new UpdateWrapper<GoodsSeckill>().setSql("stock_count=" + "stock_count-1").eq("goods_id", goodsVo.getId()).gt("stock_count",0));
        if (!flag){
            return null;
        }
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setUserId(user.getId()).setGoodsId(goodsVo.getId()).setDeliveryAddrId(0L).setGoodsName(goodsVo.getGoodsName())
                .setGoodsCount(1).setGoodsPrice(goodsVo.getGoodsPrice()).setActualPayment(goodsVo.getSeckillPrice()).setOrderChannel(1).setStatus(0).setCreateDate(new Timestamp(new Date().getTime())).setPayDate(null);
        orderInfoMapper.insert(orderInfo);
        
        Order order = new Order();
        order.setGoodsId(goodsVo.getId());
        order.setUserId(user.getId());
        order.setOrderId(orderInfo.getId());
        redisTemplate.opsForValue().set("userId:" + user.getId() + "_goodsId:" + goodsVo.getId(),order);
        orderMapper.insert(order);
        return orderInfo;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String s = MD5Util.md5(UUIDUtil.uuid() + "123456");
        redisTemplate.opsForValue().set("seckillPath:"+user.getId(),s,30, TimeUnit.SECONDS);
        return s;
    }
}
